const fs = require('fs');
const path = require('path');
const bytenode = require('bytenode');
const v8 = require('v8');

v8.setFlagsFromString('--no-lazy');
const backgroundPathName = path.join(__dirname, './dist_electron/bundled/background.js');
const preloadPathName = path.join(__dirname, './dist_electron/bundled/preload.js');
const loadContent = `'use strict';
const bytenode = require('bytenode');
const fs = require('fs');
const v8 = require('v8');
const path = require('path');
v8.setFlagsFromString('--no-lazy');`;
try {
    (async function () {
        try {
            await bytenode.compileFile({
                filename: backgroundPathName,
                electron: true,  // electron的项目这个参数一定要加上
                compileAsModule: true,
            }, `${backgroundPathName}c`);
            // 将原来的js文件里面的内容替换成下面的内容
            const backgroundContent = loadContent + `require('./background.jsc')`;
            fs.writeFileSync(backgroundPathName, backgroundContent, 'utf8');

            await bytenode.compileFile({
                filename: preloadPathName,
                electron: true,// electron的项目这个参数一定要加上
                compileAsModule: true,
            }, `${preloadPathName}c`);
            // 将原来的js文件里面的内容替换成下面的内容
            const preloadContent = loadContent + `require('./preload.jsc');`;
            fs.writeFileSync(preloadPathName, preloadContent, 'utf8');
            console.log('bytenode complete'); 
            process.exit(0); 
        } catch (e) {
            console.error(`run_bytenode_err: ${e}`);
        }
    }());
} catch (e) {
    console.error(`run_bytenode_err: ${e}`);
}
